package cn.iocoder.yudao.module.famous.doctor.controller.app.video;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.video.vo.*;
import cn.iocoder.yudao.module.famous.doctor.convert.video.FamVideoConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.video.FamVideoDO;
import cn.iocoder.yudao.module.famous.doctor.service.video.FamVideoService;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.vo.MemberUserParamDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "App - 视频管理")
@RestController
@RequestMapping("/famous.doctor/fam-video/app")
@Validated
public class AppFamVideoController {

    @Resource
    private FamVideoService famVideoService;
    @Resource
    private MemberUserService memberUserService;


    @GetMapping("/get")
    @Operation(summary = "获得视频管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<FamVideoRespVO> getFamVideo(@RequestParam("id") Long id) {
        FamVideoDO famVideo = famVideoService.getFamVideo(id);
        return success(FamVideoConvert.INSTANCE.convert(famVideo));
    }

    @GetMapping("/page")
    @Operation(summary = "获得视频管理分页")
    public CommonResult<PageResult<FamVideoRespVO>> getFamVideoPage(@Valid FamVideoPageReqVO pageVO) {
        PageResult<FamVideoDO> pageResult = famVideoService.getFamVideoPage(pageVO);
        PageResult<FamVideoRespVO> famVideoRespVOPageResult = FamVideoConvert.INSTANCE.convertPage(pageResult);
        famVideoRespVOPageResult.getList().forEach(famVideoRespVO -> {
            if (famVideoRespVO.getCreator() != null) {
                MemberUserDO user = memberUserService.getUser(Long.valueOf(famVideoRespVO.getCreator()));
                if (user!=null) {
                    famVideoRespVO.setAvatar(user.getAvatar());
                }
            }
        });
        return success(famVideoRespVOPageResult);
    }

}
